package com.service.Impl;

import com.constant.Constant;
import com.entity.Resource;
import com.entity.vo.UserResourceResultVO;
import com.entity.vo.UserResourceVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mapper.UserResourceMapper;
import com.service.UserResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;

@Service
public class UserResourceServiceImpl implements UserResourceService {

    @Autowired
    UserResourceMapper userResourceMapper;

    @Override
    public PageInfo<UserResourceResultVO> showfindBackResource(UserResourceVo userResourceVo) {
        Integer status = userResourceVo.getStatus();
        String create_start_date = userResourceVo.getCreate_start_date();
        String create_end_date = userResourceVo.getCreate_end_date();
        if(status==-1){
            userResourceVo.setStatus(null);
        }
        if (create_start_date==""){
            userResourceVo.setCreate_end_date(null);
        }
        if (create_end_date==""){
            userResourceVo.setCreate_end_date(null);
        }
        List<UserResourceResultVO> userResourceVos = userResourceMapper.selectfindBackResource(userResourceVo);

        if (userResourceVo.getPageNo()==null){
            PageHelper.startPage(1, Constant.PAGE_SIZE_COURSE);
        }
        PageInfo pageInfo=new PageInfo(userResourceVos);
        return pageInfo;
    }

    @Override
    public Integer changeToggleStatus(Integer id, Integer status) {
        Integer integer = userResourceMapper.updateToggleStatus(id, status);
        return integer;
    }

    @Override
    public ResponseEntity<byte[]> dowmFile(Integer id, HttpServletRequest request) {
        Resource resource = userResourceMapper.selectSourseInfo(id);
        String path = resource.getPath();
        String original_name = resource.getOriginal_name();
        String file_type = resource.getFile_type();
        String filename="/"+original_name+"."+file_type;
        ServletContext context = request.getServletContext();
        String realPath = context.getRealPath(path + filename);
        File file = new File(realPath);
        FileInputStream in=null;
        byte[] bytes=null;
        int len;
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setContentDispositionFormData("attachment", filename);
        ResponseEntity<byte[]> responseEntity=null;
        try {
            in=new FileInputStream(file);
            bytes=new byte[1024];
            while((len=in.read(bytes,0,bytes.length))!=-1){
                in.read(bytes);
                responseEntity= new ResponseEntity<>(bytes, httpHeaders, HttpStatus.OK);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return responseEntity;
    }
}
